package com.adinnet.api.sign.service.impl;

import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.peaseRecord.mapper.PeaseRecordMapper;
import com.adinnet.api.peaseRecord.model.PeaseRecord;
import com.adinnet.api.peaseRule.mapper.PeaseRuleMapper;
import com.adinnet.api.sign.model.Sign;
import com.adinnet.api.sign.mapper.SignMapper;
import com.adinnet.api.sign.service.SignService;
import com.adinnet.common.peaseRule.enums.PeaseRule;
import com.adinnet.common.utils.DateUtil;
import com.adinnet.core.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 * 签到记录 服务实现类
 * </p>
 *
 * @author chenjia
 * @since 2020-12-03
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private ChildMapper childMapper;

    @Autowired
    private PeaseRecordMapper peaseRecordMapper;

    @Autowired
    private PeaseRuleMapper peaseRuleMapper;

    @Override
    public List<Sign> getSignRecord(Integer year,Integer month, Integer childId) {
        Date dayOfMonth = DateUtil.getFirstDayOfMonthByParams(year,month);
        Date lastDayOfMonth = DateUtil.getLastDayOfMonthByParams(year,month);
        List<Sign> signs=signMapper.getSignRecord(dayOfMonth,lastDayOfMonth,childId);
        return signs;
    }

    @Override
    @Transactional
    public JsonResult sign(Integer childId) {
        Child cd = new Child();
        cd.setId(childId);
        Child child = childMapper.selectById(childId);
        Integer continueSignNum = child.getContinueSignNum();
        Integer continueSignDays = child.getContinueSignDays();
        Integer  continueSignPease= 0;
        String sourceName="";
        Date date = new Date();
        Date yesterdayBegin = DateUtil.getYesterdayBegin(date);
        Date yesterdayEnd = DateUtil.getYesterdayEnd(date);
        Date todayBegin = DateUtil.getDayBegin(date);
        Date todayEnd = DateUtil.getDayEnd(date);
        //判断今日是否签到
        List<Sign> todaysigns=signMapper.getSignRecord(todayBegin,todayEnd,childId);
        if(todaysigns!=null&&!todaysigns.isEmpty()){
            return JsonResult.error("签到异常：今日已签到，请勿重复签到");
        }
        //判断连续签到
        List<Sign> signs=signMapper.getSignRecord(yesterdayBegin,yesterdayEnd,childId);
        if(signs==null||signs.isEmpty()){
            continueSignNum=1;
            continueSignDays=1;
        }else if (signs.size()==1){
            continueSignNum++;
            continueSignDays++;
        }else{
            return JsonResult.error("签到异常");
        }
        if(continueSignDays==10){
            continueSignPease= peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.SIGN10.getCode());
            sourceName=PeaseRule.SIGN10.getMessage();
        }else if(continueSignDays==15){
            continueSignPease= peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.SIGN15.getCode());
            sourceName=PeaseRule.SIGN15.getMessage();
        }else if(continueSignDays==30){
            continueSignPease= peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.SIGN30.getCode());
            sourceName=PeaseRule.SIGN30.getMessage();
        }else if (continueSignDays==31){
            continueSignDays=1;
        }
        cd.setContinueSignNum(continueSignNum);
        cd.setContinueSignDays(continueSignDays);
        //生成今日签到记录
        Sign sign = new Sign();
        sign.setChildId(childId);
        sign.setSignDate(date);
        sign.setContinueSignNum(continueSignNum);//当前签到连续签到天数,前端用来判断日历上连线的效果
        sign.setPeaseNum(peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.SIGNEVERYDATE.getCode()));
        //插入每日签到金豆
        PeaseRecord peaseRecord1 = new PeaseRecord();
        peaseRecord1.setType(1);
        peaseRecord1.setChildId(childId);
        peaseRecord1.setPeaseNum(peaseRuleMapper.getPeaseRuleNumByCode(PeaseRule.SIGNEVERYDATE.getCode()));
        peaseRecord1.setSource(1);
        peaseRecord1.setSourceName(PeaseRule.SIGNEVERYDATE.getMessage());
        peaseRecordMapper.insert(peaseRecord1);
        //判断是否插入连续签到金豆
        if(continueSignPease!=0){
            PeaseRecord peaseRecord2 = new PeaseRecord();
            peaseRecord2.setType(1);
            peaseRecord2.setChildId(childId);
            peaseRecord2.setPeaseNum(continueSignPease);
            peaseRecord2.setSource(1);
            peaseRecord2.setSourceName(sourceName);
            peaseRecordMapper.insert(peaseRecord2);
        }
        signMapper.insert(sign);
        childMapper.updateById(cd);
        return JsonResult.ok();
    }

    @Override
    public Integer getIsSign(Integer childId) {
        Date date = new Date();
        Date todayBegin = DateUtil.getDayBegin(date);
        Date todayEnd = DateUtil.getDayEnd(date);
        //判断今日是否签到
        List<Sign> todaysigns=signMapper.getSignRecord(todayBegin,todayEnd,childId);
        if(todaysigns!=null&&!todaysigns.isEmpty()){
            return 0;
        }
        return 1;
    }

    /**
     * 签到列表接口获取连续签到天数，此处逻辑需要在这里判断，如果断签了，在这里
     * 需要更新连续签到天数为0，如果没有断签，直接返回连续签到天数
     * @param child
     * @return
     */
    @Override
    @Transactional
    public Integer getContinueSignNum(Child child) {
        Integer continueSignNum = child.getContinueSignNum();
        Date date = new Date();
        //今天是否签到
        Integer isSign = getIsSign(child.getId());
        Date yesterdayBegin = DateUtil.getYesterdayBegin(date);
        Date yesterdayEnd = DateUtil.getYesterdayEnd(date);
        //判断昨日是否签到
        List<Sign> yesterdaySigns=signMapper.getSignRecord(yesterdayBegin,yesterdayEnd,child.getId());
        //如果今天没有签到就要判断是否断签，如果签到过就不需要再判断是否断签了
        if(isSign==1){
            //如果断签了，更新连续签到天数和总连续签到天数为0
            if(yesterdaySigns==null||yesterdaySigns.isEmpty()){
                //如果没更新过连续签到天数，才更新
                if(continueSignNum>0){
                    continueSignNum=0;
                    Child c = new Child();
                    c.setId(child.getId());
                    c.setContinueSignDays(0);
                    c.setContinueSignNum(0);
                    childMapper.updateById(c);
                }
            }
        }
        return continueSignNum;
    }
}
